package org.jiuling.request.http.factory;

import org.jiuling.basics.enums.ApplicationLayerProtocolEnum;
import org.jiuling.basics.exception.BasicsException;
import org.jiuling.basics.util.EnumUtil;
import org.jiuling.request.exception.RequestRuntimeException;
import org.jiuling.request.http.HttpRequest;
import org.jiuling.request.http.handle.HttpRequestHandle;
import org.jiuling.request.http.handle.HttpsRequestHandle;

import java.net.URI;

/**
 * Http请求工厂
 * @author 紫灵
 * @date 2024年08月28日 11时19分11秒
 */
public interface HttpRequestFactory {

    /**
     * 开启Http请求
     * @param uriPath uri路径
     * @return http请求
     */
    static HttpRequest openHttpRequest(String uriPath){
        return openHttpRequest(URI.create(uriPath));
    }

    /**
     * 开启Http请求
     * @param uri uri
     * @return http请求
     */
    static HttpRequest openHttpRequest(URI uri){
        try {
            ApplicationLayerProtocolEnum applicationLayerProtocolEnum = EnumUtil.valueOfEnum(
                    ApplicationLayerProtocolEnum.class,
                    uri.getScheme(),
                    "name");
            switch (applicationLayerProtocolEnum){
                case HTTP -> {
                    return new HttpRequestHandle(uri);
                }
                case HTTPS -> {
                    return new HttpsRequestHandle(uri);
                }
                default -> {
                    throw new RequestRuntimeException("请使用Http或者Https协议");
                }
            }
        } catch (BasicsException e) {
            throw new RequestRuntimeException(e.getLocalizedMessage());
        }
    }

}
